package com.wits.qso.web.controller;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wits.platform.web.controller.BaseController;
import com.wits.qso.common.enumerate.BusinessType;
import com.wits.qso.entity.Aid;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.CreContent;
import com.wits.qso.entity.HomeworkH;
import com.wits.qso.entity.MaterialD;
import com.wits.qso.entity.Year;
import com.wits.qso.service.AttachmentService;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.CreContentService;
import com.wits.qso.service.MaterialdService;
import com.wits.qso.service.TeacherService;
import com.wits.qso.service.YearService;

@Controller
@RequestMapping(value = "qso/btable/list")
public class BtableListController extends BaseController
{
	protected static final Logger logger = LoggerFactory.getLogger( BtableListController.class );
	@Autowired
	private AttachmentService attachmentService = null;
	@Autowired
	private BtableDaoService btableDaoService = null;
	@Autowired
	private CreContentService creContentService = null;
	@Autowired
	private MaterialdService materialdService = null;
	@Autowired
	private YearService yearService = null;
	@Autowired
	private TeacherService teacherService = null;
	
	@RequestMapping(value = "/toList")
	public String toList(Model model) {
		model.addAttribute( "teacherName", getCurrentUser().getName() ); 
		model.addAttribute( "currentWeek", getCurrentWeeksOfYear() );
		model.addAttribute( "totalWeeks", getWeeksOfThisYear() );
		model.addAttribute( "eventDataJson", btableDaoService.getEventsJson( teacherService.findByUserId( getCurrentUser().getId() ) ) );
		
		return "qso/btableList/list";
	}
	
	@RequestMapping(value = "/view/view")
	public String toView(Model model, @RequestParam String id) {
		if (StringUtils.isNotBlank( id )) {
			Btable btable = btableDaoService.findByIdAndTeacher( id, teacherService.findByUserId( getCurrentUser().getId() ) );
			Aid aid = creContentService.findAidByBtable( btable );
			CreContent creContent = creContentService.findCreContentByBtable( btable );
			HomeworkH homeworkH = creContentService.findHomeworkHByBtable( btable );
			
			model.addAttribute( "aid", null == aid ? null : aid );
			model.addAttribute( "creContent", null == creContent ? null : creContent );
			model.addAttribute( "homeworkH", null == homeworkH ? null : homeworkH );
//			model.addAttribute( "materialD", null == creContent ? null : creContent.getMtrdId() );
			List<MaterialD> materialDs = creContentService.findMaterialDByBtable( btable );
			model.addAttribute( "materialDs", null == materialDs ? null : materialDs );			 			
			model.addAttribute( "aidAttachments", null == aid ? null : attachmentService.findByBusiness( aid.getId(),  BusinessType.AID.getValue() ) );
			model.addAttribute( "homeworkHAttachments", null == homeworkH ? null :attachmentService.findByBusiness( homeworkH.getId(),  BusinessType.HWKH.getValue() ) );

			model.addAttribute( "btable", btable );
		}

		return "qso/btableList/form";
	}
	
	protected int getCurrentWeeksOfYear() {
		int weeks = 1;
		Year year = yearService.findEnabled();
		
		if (null != year) {
			Calendar current = Calendar.getInstance();
			current.set( Calendar.HOUR_OF_DAY, 0 );
			current.set( Calendar.MINUTE, 0 );
			current.set( Calendar.SECOND, 0 );
			current.set( Calendar.MILLISECOND, 0 );
			
			Calendar start = Calendar.getInstance();
			start.setTime( year.getDateF() ); 
			
			long diff = current.getTimeInMillis() - start.getTimeInMillis();
			int dayCount = new Float( (float) diff / (24 * 60 * 60 * 1000) ).intValue();

			weeks = dayCount / 7 + ( 0 != (dayCount % 7) ? 1 : 0 );
		}
		
		return weeks;
	}
	
	protected int getWeeksOfThisYear() {
		Year year = yearService.findEnabled();
		int weeks = 1;
		
		if (null != year) {
			Calendar start = Calendar.getInstance();
			Calendar end = Calendar.getInstance();
			start.clear();
			end.clear();
			
			start.setTime( year.getDateF() );
			end.setTime( year.getDateT() );

			long diff = end.getTimeInMillis() - start.getTimeInMillis();

			int dayCount = new Float( (float) diff / (24 * 60 * 60 * 1000) ).intValue();

			weeks = dayCount / 7 + ( 0 != (dayCount % 7) ? 1 : 0 );
		}
		
		return weeks;
	}
}
